home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 497 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.1 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Bitwise Operator, ~, Help
  5. Date: 05 Jan 1996 21:42:06 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan5144206@qcd.lanl.gov>
  8. References: <4bkhg7$4fd@berry.elite.net> <4bsf7r$pn0@spanky.pls.ov.com>
  9.     <4bt51o$7nc@news2.ios.com> <4cch26$9vv@caip.rutgers.edu>
  10.     <TANMOY.96Jan4212607@qcd.lanl.gov>
  11. NNTP-Posting-Host: qcd.lanl.gov
  12. Mime-Version: 1.0
  13. Content-Type: text
  14. In-reply-to: tanmoy@qcd.lanl.gov's message of 05 Jan 1996 04:26:07 GMT
  15.  
  16. --text follows this line--
  17. In article <TANMOY.96Jan4212607@qcd.lanl.gov> tanmoy@qcd.lanl.gov
  18. (Tanmoy Bhattacharya) writes:  
  19. <snip>
  20.       It depends on the implicit type of the constant.  This type is alwais
  21.       one that one can declare a variable in (that is, the length of the
  22.       constant written in decimal, octonarie, or sedenarie digits is
  23.       irrelevant).  If no letter is uzed, decimal constants are signd and
  24.       binarie are not, and the length is of 'int'.  Otherwize the letters
  25.       "L" and "U" set its length and signedness.
  26.  
  27.    Almost. To avoid later confusion, it should probably be mentioned that
  28.    a long (or, where appropriate, unsigned long) is chosen if required to
  29.    preserve the value. Similarly, decimal need not be signed: an unsigned
  30.    will be chosen (even in preference to a longer type) if it will do. If
  31.    unsigned long does not suffice, it is an error.
  32.  
  33. I must have been under the influence of narcotics when I wrote
  34. that. In stead of trying to figure out what I meant by the above, let
  35. me just restate what is correct: an integer constant is always the
  36. first of int, unsigned int, long and unsigned long which can represent
  37. the value: the one exception being that an unsuffixed decimal integer
  38. is never an unsigned int, though it may be an unsigned long. The value
  39. is _always_ positive (a leading minus sign technically is the unary -
  40. operator applied to the positive number: note that this need not lead
  41. to a negative number if the type chosen is an unsigned type. This
  42. explains the rationale behind the exception in case of decimal
  43. integers).
  44.  
  45. In the case under discussion 0177, it is an int; and not an unsigned
  46. int as has been claimed here. The result of bitwise operations on
  47. signed types (e.g. ~0177 that was being considered) depends on the
  48. representation of negative numbers, and hence will usually not `equal'
  49. 0177600 even if there are 16 bits to an int: typically ~0177 < 0 will
  50. be true, whereas 0177600 > 0 will always be true! In two's complement
  51. arithmetic, though, ~0177 == 017760 will still be true (for 16 bit
  52. ints): because when comparing int to unsigned int, the int is
  53. converted to unsigned int.
  54.  
  55. In summary, note the following relations on a two's complement machine
  56. with 16 bit ints and longer longs.
  57.  
  58. 0177 is an int, and is the same as 127:
  59.  
  60.   0177 > 0    127 > 0
  61.  -0177 < 0   -127 < 0
  62.  
  63.   0177 == 127 
  64.  
  65. ~0177 is -128 and not 0177600 which is 65408U. Note that 65408 is
  66. 65408L instead which is altogether different:
  67.  
  68.   0177600 > 0   65408U > 0    ~0177 < 0    -128 < 0   65408 > 0
  69.  -0177600 > 0  -65408U > 0   -~0177 > 0  - -128 > 0  -65408 < 0
  70.  
  71.  0177600 == ~0177   65408U  == ~0177    65408  != ~0177
  72.  0177600 == -128    0177600 == -128     65408  != -128
  73.  0177600 == 65408   0177600 == 65408U   65408U == 65408    
  74.  
  75. In one's complement arithmetic ~0177 is -127 instead. 0177600 is still
  76. 65408U. This time, 0177600 != ~0177 because 0177600 == -128 still
  77. holds (and 0177600 != -127).
  78.  
  79. Hope this clarifies more than confuses. If it has managed to confuse
  80. someone, let me just apologize and suggest explicit uses of L and U
  81. where appropriate, and guarding against bit manipulation operators on
  82. signed quantities.
  83.  
  84. Cheers
  85. Tanmoy
  86. --
  87. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  88. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  89. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  90. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  91. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  92. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  93.